var SZModel           = require('./SZModel');
var SZProto           = require('./SZProto');
var SZLogic           = require('./SZLogic');
var RoomProto         = require('../../API/RoomProto');
var HallApi           = require('../../API/HallAPI');
var RoomAPI           = require('../../API/RoomAPI');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';

cc.Class({
	extends: cc.Component,
	properties: {
	},

	onLoad: function () {
		this.seatNames = ['北京', '上海', '广州', '天津', '重庆', '香港', '南京', '澳门', '海口', '深圳'];
		this.msgbg = cc.find('msgbg', this.node);
		this.headSprite = cc.find('head', this.node).getComponent(cc.Sprite);
		this.nameLabel = cc.find('name', this.node).getComponent(cc.Label);
		this.idLabel = cc.find('id', this.node).getComponent(cc.Label); 
		this.scoreNode = cc.find('scorebg', this.node);
		this.scoreNodePos = this.scoreNode.getPosition();
		this.scoreLabel = cc.find('gold', this.scoreNode).getComponent(cc.Label);
		this.goldNode = cc.find('icon', this.scoreNode);
		this.bankerNode = cc.find('banker', this.node);
		this.readyNode = cc.find('ready', this.node);
		this.readyNodePos = this.readyNode.getPosition();
		this.fangzhuNode = cc.find('fangzhu', this.node);
		this.light = this.node.getChildByName('light');
		this.node.active = false;

		this.winScoreNode = this.node.getChildByName('winscore');
		this.loseScoreNode = this.node.getChildByName('losescore');
		Global.MessageCallback.addListener('RoomMessagePush', this);
		Global.MessageCallback.addListener('GameMessagePush', this);
		// Global.MessageCallback.addListener('UpdateUserInfoPush', this);
		Global.MessageCallback.addListener('ReConnectSuccess', this);
	},

	showSeat: function () {
		let names = ['msgbg', 'head', 'headedg', 'fangzhu', 'name', 'id', 'robstate', 'scorebg', 'banker', 'winscore', 'losescore', 'light', 'text', 'ready', 'offline'];
		for (let name of names) {
			let node = this.node.getChildByName(name);
			if (node) {
				node.active = false;
			}
		}
		this.refrushName();
	},

	hideSeat: function () {
		if (this.seatNode) {
			this.seatNode.active = false;
		}
		let names = ['msgbg', 'head', 'headedg', 'name', 'id', 'robstate', 'scorebg', 'banker', 'winscore', 'losescore', 'ready'];
		for (let name of names) {
			let node = this.node.getChildByName(name);
			if (node) {
				node.active = true;
			}
		}
	},

	refrushName: function () {
		if (!this.seatNode) {
			let node = new cc.Node();
			node.setPosition(this.msgbg.getPosition()); 
			node.parent = this.node;  

			let bgNode = new cc.Node();
			bgNode.parent = node;
			cc.loader.loadRes('Game/Common/citybg', cc.SpriteFrame, (err, spriteFrame) => {
				if (!err) {
					bgNode.addComponent(cc.Sprite).spriteFrame = spriteFrame;
				}
				if (this.msgbg.width > this.msgbg.height) {
					bgNode.angle = -90;
					bgNode.width = this.msgbg.height;
					bgNode.height = this.msgbg.width;
				}
				else {
					bgNode.height = this.msgbg.height;
					bgNode.width = this.msgbg.width;
				}
			});

			let labelNode = new cc.Node();
			labelNode.color = new cc.Color(255, 255, 255);
			labelNode.name = 'Label'; 
			labelNode.parent = node;
			labelNode.y = 10;
			let label = labelNode.addComponent(cc.Label);
			label.fontSize = 25;
			label.string = this.seatNames[this.getChairID()];     

			bgNode.on(cc.Node.EventType.TOUCH_END, this.onSeatClick.bind(this), this); 
			this.seatNode = node;

			let arrow = new cc.Node();
			arrow.parent = node;
			arrow.y = -15;
			cc.loader.loadRes('Game/Common/jiantou', cc.SpriteFrame, (err, spriteFrame) => {
				if (!err) { arrow.addComponent(cc.Sprite).spriteFrame = spriteFrame; }
			});
			arrow.setScale(0.8);
		}
		else {
			this.seatNode.active = true;
			cc.find('Label', this.seatNode).getComponent(cc.Label).string = this.seatNames[this.getChairID()];  
		}
	},

	onSeatClick: function () {
		if (SZModel.isUnionCreate()) {
			let user = SZModel.getUserByChairID(SZModel.getMyChairID());
			if (user.userInfo.score < SZModel.getGameRule().scoreLowLimit) {
				Global.DialogManager.addTipDialog('金币不足，不能入座');
				return;
			}
		}
		Global.NetworkManager.notify(RoomMessageRouter, RoomProto.getUserChangeSeatNotify(SZModel.getMyChairID(), this.getChairID()));
	},

	setIndex: function (index) {
		this.index = index;
		if (this.index == 0 && cc.visibleRect.width/cc.visibleRect.height > 1.8) {
			this.readyNode.x = cc.visibleRect.width/2-1334/2+this.readyNodePos.x;
			this.scoreNode.x = cc.visibleRect.width/2-1334/2+this.scoreNodePos.x;
		}
	},

	getChairID: function (chairID) {
		if (SZModel.getMyChairID() >= SZModel.getChairCount()) {
			return this.index;
		}
		else {
			return (SZModel.getMyChairID()+this.index)%SZModel.getChairCount();
		}
	},

	gameInit () {
		console.log("SZHead gameInit");
		if (typeof(this.index) != 'number') { return; } /* 未使用，等待销毁 */ 
		this.node.active = true;
		let user = SZModel.getUserByChairID(this.getChairID());
		if (!user) {
			let ready = (SZModel.getUserByChairID(SZModel.getMyChairID()).userStatus&RoomProto.userStatusEnum.READY);
			if ((SZModel.getCurBureau() == 0 && !ready) || (SZModel.getMyChairID() >= SZModel.getChairCount() && SZModel.getGameRule().canEnter)) {
				this.showSeat();
			}
			else {
				this.hideSeat();
				this.node.active = false;
			}
			return;
		}
		else {
			this.hideSeat();
		}

		this.nameLabel.string = user.userInfo.nickname;
		Global.CCHelper.updateSpriteFrame(user.userInfo.avatar, this.headSprite);
		this.idLabel.string = SZModel.getCurScoreByChairID(this.getChairID());
		let is_creator = (SZModel.getRoomCreatorChairID() == this.getChairID());
		this.fangzhuNode.active = is_creator;
		this.readyNode.active = (user.userStatus&RoomProto.userStatusEnum.READY);
		this.bankerNode.active = (this.getChairID() == SZModel.getBankerChairID());
		let score = SZModel.getPourScoreByChairID(this.getChairID());
		this.setPourScore(score);
		this.showResultScore();
		cc.find('offline', this.node).active = (user.userStatus&RoomProto.userStatusEnum.OFFLINE) > 0;
		if (SZModel.getGameStatus() == SZProto.gameStatus.POUR_SCORE && this.getChairID() == SZModel.getCurChairID()) {
			this.light.active = true;
		}
		else {
			this.light.active = false;
		}
	},

	/*
	 * 设置下分
	 */
	setPourScore: function (score) {
		if (score == 0 || score == null) {
			this.scoreNode.active = false;
		}
		else {
			this.scoreLabel.string = Math.floor(score);
			this.scoreNode.active = true;
		}
	},

	messageCallbackHandler: function(router, msg) {
		if (!SZModel.getGameInited()) { return; }
		if (router === 'RoomMessagePush') {
			if(msg.type === RoomProto.GET_ROOM_SCENE_INFO_PUSH) {
				this.gameInit();
			}
			else if(msg.type === RoomProto.USER_READY_PUSH) {
				if (msg.data.chairID == SZModel.getMyChairID() && SZModel.getCurBureau() == 0) {
					this.gameInit();
				}
				if (this.node.active && msg.data.chairID == this.getChairID()) {
					this.readyNode.active = true;
					this.scoreNode.active = false;
				}
				if (this.node.active && msg.data.chairID == SZModel.getMyChairID()) {
					let user = SZModel.getUserByChairID(this.getChairID());
					if (user) {
						this.scoreNode.active = false;
						this.readyNode.active = (user.userStatus&RoomProto.userStatusEnum.READY);
						this.bankerNode.active = (SZModel.getBankerChairID() == this.getChairID());
						this.winScoreNode.active = false;
						this.loseScoreNode.active = false;
					}
				}
			}
			else if(msg.type === RoomProto.OTHER_USER_ENTRY_ROOM_PUSH) {
				if (msg.data.roomUserInfo.chairID == this.getChairID()) {
					this.gameInit();
				}
			}
			else if(msg.type === RoomProto.USER_LEAVE_ROOM_PUSH) {
				if (msg.data.roomUserInfo.chairID == this.getChairID()) {
					this.node.active = false;
				}
			}
			else if(msg.type === RoomProto.USER_CHANGE_SEAT_PUSH) {
				if (SZModel.getCurBureau() == 0) {
					this.gameInit();
				}
				else {
					if (SZModel.getMyUid() != msg.data.uid) {
						if (this.getChairID() == msg.data.fromChairID || this.getChairID() == msg.data.toChairID) {
							this.gameInit();
						}
					}
					else {
						this.gameInit();
					}
				}
			}
			else if(msg.type === RoomProto.ROOM_USER_INFO_CHANGE_PUSH) {
				let user = SZModel.getUserByChairID(this.getChairID());
				if (this.node.active && user) {
					// console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@1")
					// this.idLabel.string = SZModel.getCurScoreByChairID(this.getChairID());
				}
			}
			else if(msg.type === RoomProto.USER_OFF_LINE_PUSH) {
				if (msg.data.chairID == this.getChairID()) {
					cc.find('offline', this.node).active = true;
				}
			}
		}
		else if(router === 'GameMessagePush') {
			if(msg.type === SZProto.GAME_BANKER_PUSH) {
				this.setBanker();
			}
			else if(msg.type === SZProto.GAME_POUR_SCORE_PUSH) {
				if (msg.data.chairID == this.getChairID()) {
					// this.setPourScore(msg.data.chairScore);
					// this.idLabel.string = SZModel.getCurScoreByChairID(this.getChairID());
					this.idLabel.string = msg.data.gold;
				}
			}
			else if(msg.type === SZProto.GAME_STATUS_PUSH) {
				if (msg.data.gameStatus != SZProto.gameStatus.NONE) {
					this.readyNode.active = false;
				}
				else {
					// console.log("@@@@@@@@@@@@@@@@@@@@@@@@@@@3")
					// this.idLabel.string = SZModel.getCurScoreByChairID(this.getChairID());
				}
				if (msg.data.gameStatus == SZProto.gameStatus.SEND_CARDS) {
					let user = SZModel.getUserByChairID(this.getChairID());
					if (user) {
						this.scoreNode.active = false;
						/* this.readyNode.active = (user.userStatus&RoomProto.userStatusEnum.READY);  */
						this.readyNode.active = false;
						this.bankerNode.active = (SZModel.getBankerChairID() == this.getChairID());
						this.winScoreNode.active = false;
						this.loseScoreNode.active = false;
					}
				}
				if (SZModel.getGameStatus() == SZProto.gameStatus.POUR_SCORE && this.getChairID() == SZModel.getCurChairID()) {
					this.light.active = true;
				}
				else {
					this.light.active = false;
				}
			}
			else if(msg.type === SZProto.GAME_RESULT_PUSH) {
				if (SZModel.getUserByChairID(this.getChairID())) {
					this.showResultScore(true);
					if(msg.data.result.winScores[this.getChairID()] > 0){
						this.idLabel.string = Number(this.idLabel.string) + msg.data.result.winScores[this.getChairID()] +'';
					}
					
				}
			}
			else if(msg.type === SZProto.GAME_CHAT_PUSH) {
				this.gameChat(msg.data);
			}
			else if(msg.type === SZProto.GAME_TURN_PUSH) {
				if (SZModel.getGameStatus() == SZProto.gameStatus.POUR_SCORE && this.getChairID() == SZModel.getCurChairID()) {
					this.light.active = true;
				}
				else {
					this.light.active = false;
				}
			}
		} 
	},

	setBanker: function () {
		let is_banker = (SZModel.getBankerChairID() == this.getChairID());
		this.bankerNode.active = is_banker;
	},

	showResultScore: function (animal) {
		this.winScoreNode.active = false;
		this.loseScoreNode.active = false;
		let result = SZModel.getResult();
		if (result) {
			let score = Math.floor(result.winScores[this.getChairID()]);
			if (score > 0) {
				this.winScoreNode.active = true;
				this.winScoreNode.getComponent(cc.Label).string = '+' + Math.floor(score);
				if (animal) {
					this.winScoreNode.x = -50;
					this.winScoreNode.runAction(cc.moveTo(0.2, cc.v2(0, 22)).easing(cc.easeIn(3.0)));
				}
			}
			else if(score < 0) {
				this.loseScoreNode.active = true;
				this.loseScoreNode.getComponent(cc.Label).string = ''+Math.floor(score); 
				if (animal) {
					this.loseScoreNode.x = -50;
					this.loseScoreNode.runAction(cc.moveTo(0.2, cc.v2(0, 22)).easing(cc.easeIn(3.0)));
				}
			}
			else if(result.winners.indexOf(this.getChairID()) != -1) {
				this.winScoreNode.active = true;
				this.winScoreNode.getComponent(cc.Label).string = '+' + Math.floor(score);
				if (animal) {
					this.winScoreNode.x = -50;
					this.winScoreNode.runAction(cc.moveTo(0.2, cc.v2(0, 22)).easing(cc.easeIn(3.0)));
				}
			}
		}
	},

	/*
	 * 游戏聊天
	 */
	gameChat: function (data) {
		let node = cc.find('text', this.node);
		let label = cc.find('Label', node);
		if (data.chairID == this.getChairID()) {
			let user = SZModel.getUserByChairID(this.getChairID());
			if (cc.find('emotion', node)) {
				cc.find('emotion', node).destroy();
			}
			if (data.type == 1) {
				label.active = false;
				let emotionNode = Global.CCHelper.getChatEmotionNode(data.msg, () => { node.active = false; });
				emotionNode.parent = node;
				emotionNode.setPosition(-70, 0);
				emotionNode.scaleX = node.scaleX;
				emotionNode.name = 'emotion';
				node.setContentSize(130, 110);
				node.active = true;
			}
			else if(data.type == 2) {
				let index = ['快点啊，等到花儿都谢了！', '不要吵了，不要吵了。专心玩游戏吧！', '怎么又断线了，网络怎么这么差啊！', '不要走，决战到天亮！', '底牌亮出来绝对吓死你！', '风水轮流转，底裤都输光！', '看我通杀全场，这些钱都是我的！', '不好意思啊，我要离开一会！'].indexOf(data.msg);
				Global.AudioManager.playSound('Game/ChatAudio/' + ((user.userInfo.sex == 0)? 'man/':'woman/') +index);
				label.active = true;
				label.getComponent(cc.Label).string = data.msg;
				this.scheduleOnce(() => {
					node.width = label.width+50;
					node.height = 58;
				}, 0);
				node.active = true;
				node.stopAllActions();
				node.runAction(cc.sequence(
					cc.delayTime(2),
					cc.callFunc(() => {
						node.active = false;
					})
				));
			}
		}
	},

	onDestroy: function () {
		Global.MessageCallback.removeListener('RoomMessagePush', this);
		Global.MessageCallback.removeListener('GameMessagePush', this);
		Global.MessageCallback.removeListener('ReConnectSuccess', this);
	},
});

